﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Model;
using System.Collections;

namespace MsDAL
{
    public class Sys5a02DAL
    {
        /// <summary>
        /// 根据Listid得到整个 Sys5a02对象,如果没查到，返回null
        /// </summary>
        /// <param name="Listid">该实体主键 Listid</param>
        /// <returns>实体Sys5a02的对象</returns>
        public virtual Model.Sys5a02 GetModelById(string Listid)
        {
            string sql = @"SELECT [listid], [plistid], [action], [pkfield], [ptable], [pfield], [pwhere], [porder], [remark] 
            FROM [Sys5a02] WHERE listid=@listid";

            List<SqlParameter> parameters = new List<SqlParameter>();

            DataTable dt = DBHelper.GetData(sql, new SqlParameter("@Listid", Listid));

            //调用实体Sys5a02的GetSys5a02ListByDataTable方法得到实体对象
            List<Model.Sys5a02> list = Model.Sys5a02.GetSys5a02ListByDataTable(dt);

            return (list.Count > 0) ? list[0] : null;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public virtual SqlApp Add(Model.Sys5a02 sys5a02)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Sys5a02(");
            strSql.Append("listid ,plistid ,action ,pkfield ,ptable ,pfield ,pwhere ,porder ,remark )");
            strSql.Append(" values (");
            strSql.Append("@listid ,@plistid ,@action ,@pkfield ,@ptable ,@pfield ,@pwhere ,@porder ,@remark )");

            SqlParameter[] parameters = {
				new SqlParameter("@listid",SqlDbType.NVarChar),
				new SqlParameter("@plistid",SqlDbType.NVarChar),
				new SqlParameter("@action",SqlDbType.NChar),
				new SqlParameter("@pkfield",SqlDbType.NVarChar),
				new SqlParameter("@ptable",SqlDbType.NVarChar),
				new SqlParameter("@pfield",SqlDbType.NVarChar),
				new SqlParameter("@pwhere",SqlDbType.NVarChar),
				new SqlParameter("@porder",SqlDbType.NVarChar),
				new SqlParameter("@remark",SqlDbType.NVarChar)
				};
            parameters[0].Value = sys5a02.Listid;
            parameters[1].Value = sys5a02.Plistid;
            parameters[2].Value = sys5a02.Action;
            parameters[3].Value = sys5a02.Pkfield;
            parameters[4].Value = sys5a02.Ptable;
            parameters[5].Value = sys5a02.Pfield;
            parameters[6].Value = sys5a02.Pwhere;
            parameters[7].Value = sys5a02.Porder;
            parameters[8].Value = sys5a02.Remark;

            return new SqlApp(strSql.ToString(), new ArrayList(parameters), false, "数据保存失败！");
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public virtual SqlApp Update(Model.Sys5a02 sys5a02)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Sys5a02 set ");
            strSql.Append("plistid=@plistid ,action=@action ,pkfield=@pkfield ,ptable=@ptable ,pfield=@pfield ,pwhere=@pwhere ,porder=@porder ,remark=@remark ");
            strSql.Append("where listid=@listid ");
            SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar),
				new SqlParameter("@plistid", SqlDbType.NVarChar),
				new SqlParameter("@action", SqlDbType.NChar),
				new SqlParameter("@pkfield", SqlDbType.NVarChar),
				new SqlParameter("@ptable", SqlDbType.NVarChar),
				new SqlParameter("@pfield", SqlDbType.NVarChar),
				new SqlParameter("@pwhere", SqlDbType.NVarChar),
				new SqlParameter("@porder", SqlDbType.NVarChar),
				new SqlParameter("@remark", SqlDbType.NVarChar)
				};
            parameters[0].Value = sys5a02.Listid;
            parameters[1].Value = sys5a02.Plistid;
            parameters[2].Value = sys5a02.Action;
            parameters[3].Value = sys5a02.Pkfield;
            parameters[4].Value = sys5a02.Ptable;
            parameters[5].Value = sys5a02.Pfield;
            parameters[6].Value = sys5a02.Pwhere;
            parameters[7].Value = sys5a02.Porder;
            parameters[8].Value = sys5a02.Remark;

            return new SqlApp(strSql.ToString(), new ArrayList(parameters), false, "数据保存失败！");
        }

        public virtual SqlApp Delete(string listid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM  Sys5a02  ");
            strSql.Append("WHERE listid=@listid ");
            SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar)
				};
            parameters[0].Value = listid;

            return new SqlApp(strSql.ToString(), new ArrayList(parameters), false, "数据保存失败！");
        }

        public DataTable GetList(string plistid)
        {
            string sql = @"SELECT [listid],  [action] as taction , 'M' AS mk
            FROM [Sys5a02] WHERE plistid=@plistid";

            return DBHelper.GetData(sql, new SqlParameter("@plistid", plistid));
        }
    }
}
